package com.lightkits.wms.domain.rawmatstorage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.FndEventTypeBase;
import com.lightkits.common.db.entity.FndWmMoveTypeBase;
import com.lightkits.common.db.entity.WmDeliveryTicketLine;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ParseUtil;
import com.lightkits.framework.soa.client.id.GetGlobalIDRequestType;
import com.lightkits.framework.soa.client.id.GetGlobalIDResponseType;
import com.lightkits.framework.soa.client.id.IDServiceClient;
import com.lightkits.wms.domain.rawmatstorage.service.CreateEvevt;
import com.lightkits.wms.domain.usuallyinterface.GetWmMoveTypeExecutor;
import com.lightkits.wms.dto.rawmatstorage.model.RawMatStorageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;

/**
 * CreateEventService
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/21 2:29
 */
@Service
public class CreateEventService implements CreateEvevt {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetWmMoveTypeExecutor.class);

    private final IDServiceClient idAdapterService;

    @Autowired
    public CreateEventService(IDServiceClient idAdapterService) {
        this.idAdapterService = idAdapterService;
    }

    @Override
    public CheckResult<BdEvent> createEvent(RawMatStorageDTO dto){
        CheckResult<BdEvent> checkResult=new CheckResult<>();
        checkResult.setSuccessful(true);
        GetGlobalIDRequestType idRequest = new GetGlobalIDRequestType();
        idRequest.setCount(1);
        GetGlobalIDResponseType idResponse = idAdapterService.getGlobalId(idRequest);
        Iterator<Long> sfIds = idResponse.getIds().iterator();
        try{
           BdEvent event=new BdEvent();
           event.setEventRequestId(sfIds.next());
            FndWmMoveTypeBase moveTypeBase=new FndWmMoveTypeBase().selectOne(new EntityWrapper()
            .eq(FndWmMoveTypeBase.MOVE_TYPE_CODE,"WM_DLT_LINE_INPUT "));
            FndEventTypeBase base=new FndEventTypeBase().selectById(moveTypeBase.getEventTypeId());
            event.setEventTypeCode(base.getEventTypeCode());
            event.setEventTypeId(base.getId());
            event.setOrganizationId(ParseUtil.tryLongWithException(dto.getOrgId()));
            event.setWorkerId(ParseUtil.tryLongWithException(dto.getWorkerId()));
            event.setRemark(dto.getRemark());
            WmDeliveryTicketLine line=new WmDeliveryTicketLine().selectById(dto.getTicketLineId());
            event.setItemId(line.getItemId());
            checkResult.setData(event);
        }catch (Exception e) {
            LOGGER.error("createEvent.error", e);
            throw new LightKitsBizException(e);
        }
        return checkResult;
    }
//    @Override
//    public CheckResult<BdTransaction> createTrans(RawMatStorageDTO dto){
//        CheckResult<BdTransaction> checkResult=new CheckResult<>();
//        checkResult.setSuccessful(true);
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
//        try{
//            if(dto.getOwnerType().equals("OWN")){
//                checkResult.setSuccessful(false);
//                return checkResult;
//            }
//            FndWmMoveTypeBase moveTypeBase=new FndWmMoveTypeBase().selectOne(new EntityWrapper()
//            .eq(FndWmMoveTypeBase.MOVE_TYPE_CODE,"WM_DLT_LINE_INPUT "));
//            FndTransactionTypeBase transactionTypeBase=new FndTransactionTypeBase().selectById(moveTypeBase.getTransactionTypeId());
//            if(ObjectUtil.isEmpty(transactionTypeBase)){
//                checkResult.setSuccessful(false);
//                return checkResult;
//            }
//            WmDeliveryTicketLine line=new WmDeliveryTicketLine().selectById(dto.getTicketLineId());
//            BdTransaction trans=new BdTransaction();
//            trans.setTransactionTypeId(transactionTypeBase.getId());
//            trans.setTransactionTypeCode(transactionTypeBase.getTransactionTypeCode());
//            trans.setOrganizationId(ParseUtil.tryLongWithException(dto.getOrgId()));
//            trans.setItemId(line.getItemId());
//            trans.setTransactionQty(new BigDecimal(dto.getInventoryQty()));
//            trans.setTransactionUom(line.getUom().toString());
//            trans.setTransactionDate(format.parse(format.format(new Date())));
//            format.applyPattern("HH:mm:ss");
//            trans.setTransactionTime(format.parse(format.format(new Date())));
//            BdDocument document=new BdDocument().selectOne(new EntityWrapper()
//            .eq(BdDocument.DOCUMENT_TYPE_CODE,"WM_DELIVERY_TICKET"));
//            trans.setDocumentTypeId(document.getDocumentTypeId());
//            trans.setDocumentTypeCode("WM_DELIVERY_TICKET");
//            trans.setDocumentId(line.getTicketId());
//            trans.setDocumentLineId(line.getId());
//            WmDeliveryTicket head=new WmDeliveryTicket().selectById(line.getTicketId());
//            trans.setDocumentNum(head.getTicketNum());
//            trans.setLotNumber(line.getLotCode());
//            trans.setWarehouseId(line.getReceiveWarehouseId());
//            trans.setWmAreaId(line.getReceiveWmAreaId());
//            trans.setToWarehouseId(ParseUtil.tryLongWithException(dto.getInventoryWarehouseId()));
//            trans.setToWmAreaId(ParseUtil.tryLongWithException(dto.getInventoryWmAreaId()));
//            trans.setWorkerId(ParseUtil.tryLongWithException(dto.getWorkerId()));
//            FndWorker worker=new FndWorker().selectById(dto.getWorkerId());
//            trans.setWorkerNumber(worker.getWorkerNumber());
//            if(!ObjectUtil.isNullOrEmpty(dto.getTransReasonCode())){
//                trans.setTransactionReasonCode(ParseUtil.tryLongWithException(dto.getTransReasonCode()));
//            }
//            trans.setRemark(dto.getRemark());
//            checkResult.setData(trans);
//        }catch (Exception e) {
//            LOGGER.error("createTrans.error", e);
//            throw new LightKitsBizException(e);
//        }
//        return checkResult;
//    }
}
